package cn.wx.scholar.core.service.importNeo4j;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import com.csvreader.CsvReader;

public class FpReadCsv {

	
	public static void main(String[] args) {
		
		final String filePath = "D:\\my_read_csv\\expertPaperFieldCount.csv";
		try {
			
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			
			StringBuffer sb =null;
			BufferedWriter output = null;
			// 读表头
            csvReader.readHeaders();
            int i = 0;
            int zj = 0;
            while (csvReader.readRecord()){
            	
            	if(i == 0) {
            		File of = new File("D:\\my_read_csv\\expertPaperFieldCount\\expertPaperFieldCount_"+ zj+".csv");
        			FileOutputStream ofs = new FileOutputStream(of,true);
        			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
        			output = new BufferedWriter(ofw);
        			
        			sb = new StringBuffer();
        			
        			//论文分片
        			//String header = "\"nId\",\"title\",\"citations\",\"year\", \"url\",\"fAbstract\",\"publisher\",\"Authors\",\"docType\"";
        			
        			String header ="\"expertId\",\"fosId\",\"paperCount\"";
        			
					sb.append(header);
					
					output.write(sb.toString());
					output.newLine();
        			
            	}
            	
            	
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				
				output.write(line);
				output.newLine();
				output.flush();
				if(i>200) {
					i=0;
					zj++;
				}
				
            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
